package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class MVSpecialCompleteAttrHandler extends AbstractAttrHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        if(KiteStringUtils.isEmpty(value)){
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        String businessType = querySceneInstService.getExtParameterValue(sceneInstId, "businessType");
        KiteAttr allCommAttr = querySceneInstService.getCommonAttr(sceneInstId, LocalKeyConsts.WHOLE_PROCESS_TIME);
        // 修改A端完成时间的值
        if (LocalKeyConsts.MV_REPLACE_A.equals(fieldName)&&null!=allCommAttr.getValue()) {
            if (Objects.nonNull(allCommAttr)) {
                if (DateFormatUtils.compareDate(value, allCommAttr.getValue(), LocalKeyConsts.DATE_FORMAT) > 0) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("CE端要求完成时间不能晚于全程要求完成时间");
                }
            }
            KiteAttr aCommAttr = querySceneInstService.getCommonAttr(sceneInstId, LocalKeyConsts.REPLACE_A);
            if (Objects.nonNull(aCommAttr)) {
                aCommAttr.setValue(value);
                aCommAttr.setValueDesc(valueDesc);
                this.checkTime(businessType, aCommAttr);
            }
        }
        // 修改Z端完成时间的值
        if (LocalKeyConsts.MV_REPLACE_Z.equals(fieldName)) {
            if (Objects.nonNull(allCommAttr)&&null!=allCommAttr.getValue()) {
                if (DateFormatUtils.compareDate(value, allCommAttr.getValue(), LocalKeyConsts.DATE_FORMAT) > 0) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("PE端要求完成时间不能晚于全程要求完成时间");
                }
            }
            KiteAttr bCommAttr = querySceneInstService.getCommonAttr(sceneInstId, LocalKeyConsts.REPLACE_Z);
            if (Objects.nonNull(bCommAttr)) {
                bCommAttr.setValue(value);
                bCommAttr.setValueDesc(valueDesc);
                this.checkTime(businessType, bCommAttr);
            }
        }
        // 全程要求完成时间
        KiteAttr allFinishAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10012");
        if (Objects.nonNull(allFinishAttr)&&null!=allFinishAttr.getValue()) {
            if (DateFormatUtils.compareDate(value, allFinishAttr.getValue(), DateFormatUtils.DATE_FORMAT) > 0) {
                ErrorConsts.BUSI_COMMON_ERROR.throwOut("PE、CE端要求完成时间不能晚于全程要求完成时间");
            }
            if (DateFormatUtils.compareDate(DateFormatUtils.getFormatedDate(), value, DateFormatUtils.DATE_FORMAT) > 0) {
                ErrorConsts.BUSI_COMMON_ERROR.throwOut("PE、CE端要求完成时间不能早于当前时间");
            }
        }

        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }

    public void checkTime(String type, KiteAttr ...attr) {
        if (Objects.isNull(attr)) {
            return;
        }
        List<KiteAttr> kiteAttrList = Arrays.asList(attr);
        if (KiteListUtils.isNotEmpty(kiteAttrList)) {
            kiteAttrList.forEach(kiteAttr -> {
                String currDate = kiteAttr.getValue();
                String originDate = kiteAttr.getOriginValue();
                // 如果加急，不能晚于之前的完成时间
                if ("URGENT".equals(type) && KiteStringUtils.isNotEmpty(originDate)) {
                    if (DateFormatUtils.compareDate(currDate, originDate, LocalKeyConsts.DATE_FORMAT) > 0) {
                        BssException.throwOut(KeyConsts.LOG_MODULE, "","加急，要求完成时间不能晚于之前的完成时间");
                    }
                }
                // 如果是延期，不能早于之前的完成时间
                if ("POSTPONE".equals(type) && KiteStringUtils.isNotEmpty(originDate)) {
                    if (DateFormatUtils.compareDate(currDate, originDate, LocalKeyConsts.DATE_FORMAT) < 0) {
                        BssException.throwOut(KeyConsts.LOG_MODULE, "","延期，要求完成时间不能早于之前的完成时间");
                    }
                }
                // 校验全程要求完成时间大于当前时间
                if ("ORD10012".equals(kiteAttr.getFieldName()) && (DateFormatUtils.compareDate(currDate, DateFormatUtils.getFormatedDate(), LocalKeyConsts.DATE_FORMAT) < 0)) {
                    BssException.throwOut(KeyConsts.LOG_MODULE, "","全程要求完成时间不能小于当前时间");
                }
            });
        }
    }
}
